<?php $base_path = Yii::app()->baseUrl; ?>
<script type="text/javascript" src="<?php echo $base_path ?>/js/admin/jquery.tablednd.js"></script>
<script type="text/javascript">
    $(function(){
        var listId = new Array;
        $('#listCate').tableDnD({
            onDrop: function(table, row) {
                listId = new Array;
                var rows = table.tBodies[0].rows;                   
                for (var i=1; i<=rows.length; i++) {                                         
                    listId.push($(rows[i-1]).attr('id') + "-" + i)
                }
                $('#listId').val(listId.join(':'));                
                $('.tableactions').show();
            }
        });
    });
</script>
<div class="block">

    <div class="block_head">
        <div class="bheadl"></div>
        <div class="bheadr"></div>
        <h2>Danh mục</h2>
        <ul>            
            <?php
            $params = array();
            if (isset($_GET['parent_id'])) {
                $params['parent_id'] = $_GET['parent_id'];
            }
            ?>
            <li>
                <?php
                if (isset($_GET['parent_id'])) {
                    ?>
                    <a href="<?php echo $this->createUrl('category/index') ?>">Danh sách danh mục cha</a> || 
                    <?php
                }
                ?>
                <a href="<?php echo $this->createUrl('category/add', $params) ?>">Thêm mới danh mục</a>
            </li>
        </ul>
    </div>
    <div class="block_content">
        <?php echo showMessage(); ?>
        <form action="" method="post">
            <table id="listCate" cellpadding="0" cellspacing="0" width="100%" class="sortable">
                <thead>
                    <tr>
                        <th>Tên danh mục</th>
                        <?php
                        if (!isset($_GET['parent_id'])) {
                            ?>
                            <th>Danh mục con</th>
                            <?php
                        }
                        ?>
                        <th>Banner</th>
                        <th>Icon</th>
                        <th>Status</th>
                        <td>&nbsp;</td>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    //id, link_title, alias, created, weight, contact_form, status
                    if (!empty($data['listItem'])) {
                        foreach ($data['listItem'] as $item) {
                            ?>
                            <tr id="<?php echo $item->id; ?>">                                
                                <td><a href="<?php echo $this->createUrl('category/edit', array('id' => $item->id)) ?>"><?php echo $item->name ?></a></td>
                                <?php
                                if (!isset($_GET['parent_id'])) {
                                    ?>
                                    <td><a href="<?php echo $this->createUrl('category/sub', array('parent_id' => $item->id)) ?>">Danh mục con</a></td>
                                    <?php
                                }
                                ?>
                                <td>
                                    <?php
                                    if (!empty($item->banner)) {
                                        echo '<img src="' . $base_path . '/upload/category/banner/' . $item->banner . '" style="max-width: 150px; max-height: 150px;" />';
                                    }
                                    ?>
                                </td>
                                <td>
                                    <?php
                                    if (!empty($item->icon)) {
                                        echo '<img src="' . $base_path . '/upload/category/icon/' . $item->icon . '" style="max-width: 100px; max-height: 100px;" />';
                                    }
                                    ?>
                                </td>
                                <td><?php echo $item->status == 1 ? 'Hiển thị' : 'Ẩn'; ?></td>
                                <td class="delete">
                                    <a href="<?php echo $this->createUrl('category/edit', array('id' => $item->id)) ?>">Sửa</a> || 
                                    <a href="<?php echo $this->createUrl('category/delete', array('id' => $item->id)) ?>" onclick="return confirm('Bạn chắc chắc chắn muốn xóa danh mục đã chọn');">Xóa</a>
                                </td>
                            </tr>
                            <?php
                        }
                    } else {
                        ?>
                        <tr><td colspan="<?php echo isset($_GET['parent_id']) ? 5 : 6; ?>">No data</td></tr>
                        <?php
                    }
                    ?>                    
                </tbody>
            </table>   
            <input type="hidden" name="listId" value="" id="listId" readonly="" />
            <div class="tableactions" style="display: none;">
                <input type="submit" class="submit tiny" value="Cập nhật" />
            </div>    
        </form>

    </div>
    <div class="bendl"></div>
    <div class="bendr"></div>
</div>